package com.bootdo.wx.service.impl;

import cn.hutool.core.map.MapUtil;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.dao.MemberFollowDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.MemberFollowDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberFollowService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;



@Service
public class MemberFollowServiceImpl implements MemberFollowService {
	@Autowired
	private MemberFollowDao memberFollowDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public MemberFollowDO get(String id){
		return memberFollowDao.get(id);
	}
	
	@Override
	public List<MemberFollowDO> list(Map<String, Object> map){
		return memberFollowDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberFollowDao.count(map);
	}
	
	@Override
	public int save(MemberFollowDO memberFollow){
		return memberFollowDao.save(memberFollow);
	}
	
	@Override
	public int update(MemberFollowDO memberFollow){
		return memberFollowDao.update(memberFollow);
	}
	
	@Override
	public int remove(String id){
		return memberFollowDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberFollowDao.batchRemove(ids);
	}

	@Override
	public R setFollow(Map<String, Object> map) {
		List<MemberFollowDO> followDOList = memberFollowDao.list(map);
		if (followDOList.size() != 0) {  //已经关注
			for (MemberFollowDO followDO : followDOList) {
				memberFollowDao.remove(followDO.getId());
			}
			return R.ok("取消关注");
		} else {
			MemberFollowDO followDO = new MemberFollowDO();
			followDO.setId(UuidUtil.get32UUID());
			followDO.setUserId(map.get("userId").toString());
			followDO.setGoodsId(map.get("goodsId").toString());
			followDO.setCreateAt(new Date());
			if (memberFollowDao.save(followDO) > 0) {
				return R.ok("关注成功");
			} else {
				return R.error();
			}
		}
	}

	@Override
	public PageInfo<Map<String,Object>> getFollowList(Map<String, Object> map) {
		String userId = com.bootdo.common.utils.StringUtils.getString(map.get("userId"));
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = 0;
		if (memberUserDO != null) {
			map.put("level", memberUserDO.getLevel());
			level = memberUserDO.getLevel();
		} else {
			map.put("level", 0);
			level = 0;
		}
		PageHelperUtils.page(map);
		List<Map<String, Object>> goodsList = memberFollowDao.getFollowList(map);
		for (Map<String, Object> goodsMap : goodsList) {
			int isvip = Integer.valueOf(goodsMap.get("isvip").toString());
			Object price = goodsMap.get("price");
			Object price_c = goodsMap.get("price_c");
			Object price_z = goodsMap.get("price_z");
			Object price_g = goodsMap.get("price_g");
			if (isvip == 0) {
				goodsMap.put("minePrice",price);
			} else {
				switch (level) {
					case 1 :
						goodsMap.put("minePrice",price_c);
						break;
					case 2 :
						goodsMap.put("minePrice",price_z);
						break;
					case 3 :
						goodsMap.put("minePrice",price_g);
						break;
					default:
						goodsMap.put("minePrice",price);
				}
			}
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goodsList);
		return pageInfo;
	}

    @Override
    public R getHistoryList(Map<String, Object> params) {
		//分组集合
		List<String> groupList = new ArrayList<>();
		PageHelperUtils.page(params);
		List<Map<String,Object>> list = memberFollowDao.getDaysList(params);
		if(list.size()<=0){
			return R.ok().put("data", new PageInfo<>(list));
		}else{
			for (Map<String, Object> stringObjectMap : list) {
				Map<String,Object> map = new HashMap<>();
				String days = stringObjectMap.get("days").toString();
				params.put("date",stringObjectMap.get("days").toString());
				params.put("userId", MapUtil.getStr(params,"userId"));
				List<Map<String,Object>> goodsList = memberFollowDao.getListByDays(params);
				map.put("goodsList",goodsList);
				stringObjectMap.put("goods",map);
			}
		}
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return R.ok().put("data",pageInfo);

    }

}
